<?php

namespace app\admin\controller\depot;

// 引入基类控制器
use app\common\controller\Backend;

// 供应商
class Supplier extends Backend
{
  public function __construct()
  {
    parent::__construct();

    $this->model = model('Depot.Supplier');
  }

  public function index()
  {
    if ($this->request->isAjax()) {
      // 过滤请求参数中的html标签和取消两边你空白
      $this->request->filter(['strip_tags', 'trim']);
      // 获取查询参数
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->model
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->count();

      $list = $this->model
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      $result = [
        'rows' => $list,
        'total' => $total
      ];

      return json($result);
    }

    return $this->view->fetch();
  }

  public function add()
  {
    if ($this->request->isPost()) {
      $params = $this->request->param('row/a');

      $data = [
        'name' => $params['name'],
        'mobile' => $params['mobile'],
        'address' => $params['address'],
      ];

      $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

      if (!empty($parentpath)) {
        $arr = explode(',', $parentpath);

        $data['province'] = isset($arr[0]) ? $arr[0] : null;
        $data['city'] = isset($arr[1]) ? $arr[1] : null;
        $data['district'] = isset($arr[2]) ? $arr[2] : null;
      }

      $result = $this->model->validate('common/Depot/Supplier')->save($data);

      if ($result) {
        $this->success('添加供应商成功');
        exit;
      } else {
        $this->error($this->model->getError());
        exit;
      }
    }

    return $this->view->fetch();
  }

  public function edit($ids = NULL)
  {
    // 查询是否存在
    $row = $this->model->find($ids);

    if (!$row) {
      $this->error('供应商不存在');
      exit;
    }

    if ($this->request->isPost()) {
      $params = $this->request->param('row/a');

      $data = [
        'id' => $ids,
        'name' => $params['name'],
        'mobile' => $params['mobile'],
        'address' => $params['address'],
      ];

      $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

      if (!empty($parentpath)) {
        $arr = explode(',', $parentpath);

        $data['province'] = isset($arr[0]) ? $arr[0] : null;
        $data['city'] = isset($arr[1]) ? $arr[1] : null;
        $data['district'] = isset($arr[2]) ? $arr[2] : null;
      }

      $result = $this->model->validate('common/Depot/Supplier')->isUpdate(true)->save($data);

      if ($result) {
        $this->success('编辑供应商成功');
        exit;
      } else {
        $this->error($this->model->getError());
        exit;
      }
    }

    $this->assign('row', $row);

    return $this->view->fetch();
  }

  public function del($ids = NULL)
  {
    $ids = empty($ids) ? [] : explode(',', $ids);
    $list = $this->model->whereIn('id', $ids)->select();

    foreach ($list as $v) {
      if (!in_array($v['id'], $ids)) {
        $this->error('没有找到供应商');
        exit;
      }
    }

    // var_dump($list);
    // exit;

    // 删除
    $result = $this->model->whereIn('id', $ids)->delete();

    if ($result === false) {
      $this->error($this->model->getError());
      exit;
    } else {
      $this->success('删除成功');
      exit;
    }
  }
}
